package com.tjw.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.tjw.dao.PermissionDao;
import com.tjw.dao.RoleDao;
import com.tjw.dao.UserDao;
import com.tjw.pojo.Permission;
import com.tjw.pojo.Role;
import com.tjw.pojo.User;
import com.tjw.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Set;


/**
 * @author tjw
 * @Date 2020/10/17
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;

    @Override
    public User findByUsername(String username) {
        User user = userDao.findByUsername(username);
        Integer userId = user.getId();
        Set<Role> roles = roleDao.findByUserId(userId);
        if (roles != null && roles.size() > 0) {
            for (Role role : roles) {
                Integer roleId = role.getId();
                Set<Permission> permissions = permissionDao.findByRoleId(roleId);
                role.setPermissions(permissions);
            }
        }
        user.setRoles(roles);
        return user;
    }
}
